package gui;

import java.util.concurrent.CountDownLatch;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;


/**
 * Logging Dialog ============== presents stores table for selection by user
 * program is initiated by selected store's view
 */
public class importDialog extends org.eclipse.swt.widgets.Dialog {
	private static Display display;
	private static importDialog inst;
	private static Shell shell;
	private static Shell dialogShell;
	private static Button logDialogButtonExit;
	private static Button logDialogButtonStart;
	private static Group logDialogGroup;

	private static Text logUserText;
	
	private static Button importDialogButtonchoose;
	public static String filePath;
	
	public static boolean successful = false;

	static Image background = new Image(display,Main.class.getResourceAsStream("import-back.png"));
	/**
	 * Auto-generated main method to display this org.eclipse.swt.widgets.Dialog
	 * inside a new Shell.
	 */
	public static void openLogDialog() {
		try {
			display = Display.getDefault();
			shell = new Shell(display);
			shell.setLocation(Main.getCentral_x() - 306,
					Main.getCentral_y() - 358);
			shell.setSize(398, 200);
			shell.setBackgroundImage(background);
			inst = new importDialog(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void closeInitDialog() {
		shell.close();
	}

	public importDialog(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM
					| SWT.APPLICATION_MODAL);

			dialogShell.setText("Music Box - Import");
			dialogShell.setBackgroundImage(background);
			dialogShell.setLayout(null);
			{
				logDialogGroup = new Group(dialogShell, SWT.NONE);
				logDialogGroup.setLayout(null);
				logDialogGroup.setBackgroundImage(background);
				logDialogGroup.setBounds(0, -8, 398, 200);
				{

					logUserText = new Text(logDialogGroup, SWT.BORDER);
					logUserText.setBounds(140, 67, 145, 22);

				}
				
				{
					importDialogButtonchoose = new Button(logDialogGroup, SWT.PUSH
							| SWT.CENTER);
					importDialogButtonchoose.setText("Choose...");
					importDialogButtonchoose.setBounds(290, 66, 70, 23);
					importDialogButtonchoose.addListener(SWT.Selection,
							new Listener() {
								public void handleEvent(Event event) {
									FileDialog fd = new FileDialog(dialogShell, SWT.OPEN);
							        fd.setText("Open");
							        fd.setFilterPath("C:/");
							        String[] filterExt = { "*.bz2", "*.*" };
							        fd.setFilterExtensions(filterExt);
							        String selected = fd.open();
							        logUserText.setText(selected);
							        filePath=logUserText.getText();
								}
							});

				}
				{
					logDialogButtonStart = new Button(logDialogGroup, SWT.PUSH
							| SWT.CENTER);
					logDialogButtonStart.setText("Import");
					logDialogButtonStart.setBounds(75, 140, 110, 35);
					logDialogButtonStart.addListener(SWT.Selection,
							new Listener() {
								public void handleEvent(Event event) {
									String user = logUserText.getText();
									if (user == "") {
										MessageBox messageBox = new MessageBox(
												shell, SWT.OK
														| SWT.ICON_WARNING
														| SWT.CANCEL);
										messageBox
												.setMessage("Enter file path");
										messageBox.open();
									
									} else {
										Main.signal = new CountDownLatch(1);
										System.out.println(filePath);
										Main.dbGui.updateDB(filePath);
										
										try {
											Main.signal.await();
										} catch (InterruptedException e) {
											MessageBox messageBox = new MessageBox(
													importDialog.shell, 32);
											messageBox.setText("Error");
											messageBox.setMessage("Error: "
													+ e.getLocalizedMessage());
											messageBox.open();
											e.printStackTrace();
										}
										
										String guiErrorMsg = Main.guiInt.getMassage();
										if (guiErrorMsg != null){
											MessageBox messageBox = new MessageBox(
													importDialog.shell, 32);
											messageBox.setText("Login Form");
											messageBox.setMessage("Error: "
													+ guiErrorMsg);
											messageBox.open();
										} else{
										MessageBox messageBox = new MessageBox(
												shell, SWT.OK | SWT.CANCEL);
										messageBox.setText("Import");
										messageBox.setMessage("Import successful: "
												+ logUserText.getText());
										messageBox.open();
										successful=true;
										shell.close();
									}
								}
								}
							});

				}
				{
					logDialogButtonExit = new Button(logDialogGroup, SWT.PUSH
							| SWT.CENTER);
					logDialogButtonExit.setText("Cancel");
					logDialogButtonExit.setBounds(210, 140, 110, 35);
					logDialogButtonExit.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							shell.close();
						}
					});
//					logDialogButtonExit.addListener(SWT.Selection,
//							new Listener() {
//						public void handleEvent(Event event) {
//							NewUserDialog.openNewUserDialog();
//					}
//				});
				}
			}
			dialogShell.layout();
			dialogShell.pack();
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			Display display = dialogShell.getDisplay();

			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Shell getDialogShell() {
		return dialogShell;
	}

	public static void setDialogShell(Shell dialogShell) {
		importDialog.dialogShell = dialogShell;
	}

	public static Button getInitDialogButtonExit() {
		return logDialogButtonExit;
	}

	public static void setInitDialogButtonExit(Button logDialogButtonExit) {
		importDialog.logDialogButtonExit = logDialogButtonExit;
	}

	public static Button getInitDialogButtonStart() {
		return logDialogButtonStart;
	}

	public static void setInitDialogButtonStart(Button logDialogButtonStart) {
		importDialog.logDialogButtonStart = logDialogButtonStart;
	}

	public static Group getInitDialogGroup() {
		return logDialogGroup;
	}

	public static void setInitDialogGroup(Group logDialogGroup) {
		importDialog.logDialogGroup = logDialogGroup;
	}

	public static Display getInitDisplay() {
		return display;
	}

	public static void setDisplay(Display display) {
		importDialog.display = display;
	}

	public static Display getDisplay() {
		return display;
	}

	public static importDialog getInst() {
		return inst;
	}

	public static Shell getShell() {
		return shell;
	}
}
